<?php

/**
 * Class name is ConfigConstantSearch * @package backend\modules\common\controllers;
 * @author  Womtech  email:chareler@163.com
 * @DateTime 2020-03-06 18:48 
 */

namespace common\models\config;

use yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\config\ConfigConstant;


/**
 * ConfigConstantSearch represents the model behind the search form of `common\models\config\ConfigConstant`.
 */
class ConfigConstantSearch extends ConfigConstant
{


    public $start;
    public $end;
    public $pageSize;
    public $page;

    //bindValues([':name'=>'ss',':id'=>1])
    protected $arrBindValues=[];

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'status', 'created_at', 'updated_at', 'created_id', 'updated_id'], 'integer'],
            [['name', 'name_en', 'description'], 'safe'],

            [['start', 'end'],  'date', 'format' => 'yyyy-MM-dd', 'on' => 'search'],
            [['pageSize', 'page'], 'integer', 'on' => 'search'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }
    /**
     * 创建时间  如果不需要或者该数据模型 没有 created_at 字段，您应该删除
     * @return array|false|int
     */
    public function getCreatedAt()
    {
        if (empty($this->created_at)) {
            return null;
        }
        $createAt = is_string($this->created_at) ? strtotime($this->created_at) : $this->created_at;
        if (date('H:i:s', $createAt) == '00:00:00') {
            return [$createAt, $createAt + 3600 * 24];
        }
        return $createAt;
    }
    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        //获取所有参数
        $this->load($params, '');

        //验证过滤及转义,系统自带规则,自定义规则,数据转化规则,都在rule中
        if (!$this->validate()) {
            return;
        }


        //查询执行开始
        //选用查询方式createCommand, 准备相关数据:分页相关,语句构成
        $rsCount =0;
        $rs =[];


        //初始化分页参数
        if (empty($this->pageSize)) {
            $this->pageSize = yii::$app->params['pageSize'];
        }
        $this->page =  !empty($this->page) ? (int)$this->page - 1 : 0;
        $rsStart = $this->page * $this->pageSize;
        //$rsEnd = ($this->page+1) * $this->pageSize;


        //sql语句,统计总数语句和查询语句的组成
        $sql = "";
        $countSql="";
        $sqlFields="";
        $sqlSelect = ""; 
        $sqlWhere =" where 1=1 ";   
        $sqlLimit ="";
        $sqlOrder ="";

        
        $sql .="select * from {{%config_constant}}";
        $sqlLimit =" limit $rsStart, $this->pageSize ";
        


        $sql .= $sqlSelect;
        $sql .= $this->toSqlWhere($params);
        $sql .= $sqlLimit;
        $sql .= $sqlOrder;

        $countSql .="select count(*) from {{%config_constant}}";
        $countSql .= $this->toSqlWhere($params);

        $sqlData = null;
        //$sqlData = yii::$app->db->createCommand($sql)->queryAll();


        //执行sql语句,获得数据
        $sqlData = yii::$app->db->createCommand($sql)->bindValues($this->arrBindValues)->queryAll();

        $sqlStr = yii::$app->db->createCommand($sql)->bindValues($this->arrBindValues)->getRawSql();
        //$sqlData = yii::$app->db->createCommand($sql)->bindValues([])->getRawSql();

        //$rsCount = yii::$app->db->createCommand($countSql)->queryScalar();
        $rsCount = yii::$app->db->createCommand($countSql)->bindValues($this->arrBindValues)->queryScalar();
        //$rsCount = $sqlData->count();
        $rs['count'] = !empty($rsCount) ? $rsCount : 0;
        $rs['data'] = $sqlData;

        //查询执行结束


        //返回数据,格式array
        return $rs;


        

        


        
    } 

    protected function toSqlWhere($params){
        $sqlWhere =" where 1=1 ";
        if (!empty($this->start)) {
            $start = $this->start;
            $startTime = strtotime($start . " 00:00:00");
            //$sqlWhere .= " and created_at >= $startTime" ;
            $sqlWhere .= " and created_at >= :startTime " ;
            $this->arrBindValues[':startTime'] = $startTime ;
        }

        if (!empty($this->end)) {
            $end = $this->end;
            $endTime = strtotime($end . " 23:59:59");
            //$sqlWhere .= " and created_at <= $endTime" ;
            $sqlWhere .= " and created_at <= :endTime" ;
            $this->arrBindValues[':endTime']= $endTime;
        }

        if (!empty($this->name)) {
            $name = $this->name;
            
            //$sqlWhere .= " and name like '%$name%'" ;
            $sqlWhere .= " and name like :name" ;
            $this->arrBindValues[':name']= '%'.$name.'%';
        }
        
        return $sqlWhere;

    }


    public function search_bak($params)
    {
        /* $sql="";
        $sqlSelect = "";
        $sql .= "select ***";
        $sqlWhere ="";
        $sql .= $sqlWhere;
        $sqlLimit ="";
        $sql .= $sqlLimit;
        $sqlOrder ="";
        $sql .= $sqlOrder;
        $sqlData = null;
        $sqlData = yii::$app->db->createCommand('SELECT id,`name` FROM poststatus')->queryAll(); */


        //选用查询方式

        //获取所有参数

        //过滤及转义

        //执行

        //返回




        $query = ConfigConstant::find()->with('creater','updater');

        // add conditions that should always apply here

        /* $dataProvider = new ActiveDataProvider([
            'pagination' => ['pageSize' => 10,],
            'query' => $query,
        ]); */

        $this->load($params, '');

        if (!$this->validate()) {
            return;
        }

        if (empty($this->pageSize)) {
            $this->pageSize = yii::$app->params['pageSize'];
        }
        $this->page =  !empty($this->page) ? (int)$this->page - 1 : 0;
        if (!empty($this->start)) {
            $start = $this->start;
            $startTime = strtotime($start . " 00:00:00");
            $query->andFilterWhere(['>=', 'created_at', $startTime]);
        }

        if (!empty($this->end)) {
            $end = $this->end;
            $endTime = strtotime($end . " 23:59:59");
            $query->andFilterWhere(['<=', 'created_at', $endTime]);
        }



        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'created_id' => $this->created_id,
            'updated_id' => $this->updated_id,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'name_en', $this->name_en])
            ->andFilterWhere(['like', 'description', $this->description]);
        
        $dataProvider = new ActiveDataProvider([
            'pagination' => [
                'pageSize' => $this->pageSize,
                'page' => $this->page,

            ],
            'query' => $query,
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]],
        ]);


        return $dataProvider;
    } 




    /* public function searchbak($params)
    {
        

        $this->load($params, '');

        if (!$this->validate()) {
            return;
        }

        if (empty($this->pageSize)) {
            $this->pageSize = yii::$app->params['pageSize'];
        }
        $this->page =  !empty($this->page) ? (int)$this->page - 1 : 0;
        if (!empty($this->start)) {
            $start = $this->start;
            $startTime = strtotime($start . " 00:00:00");
            $query->andFilterWhere(['>=', 'created_at', $startTime]);
        }

        if (!empty($this->end)) {
            $end = $this->end;
            $endTime = strtotime($end . " 23:59:59");
            $query->andFilterWhere(['<=', 'created_at', $endTime]);
        }



        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'created_id' => $this->created_id,
            'updated_id' => $this->updated_id,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'name_en', $this->name_en])
            ->andFilterWhere(['like', 'description', $this->description]);
        
        $dataProvider = new ActiveDataProvider([
            'pagination' => [
                'pageSize' => $this->pageSize,
                'page' => $this->page,

            ],
            'query' => $query,
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]],
        ]);


        return $dataProvider;
    } */
}
